﻿//copyright crl zzmmcrl@163.com;
import flash.filters.DropShadowFilter;
import mx.transitions.Tween;
import mx.transitions.easing.*;
class crl.window extends MovieClip {
	//private var __x:Number;
	private var _mc:MovieClip;
	private var _head:MovieClip;
	private var _pic;
	public var isScale:Boolean;
	private var picListenner:Object;
	private var _Loader:MovieClipLoader;
	public var W:Number, H:Number, S:Number, HColor, Cont:String, data:String, Tit:String;
	private var mTextFormat:TextFormat;
	private var _loadMC:MovieClip;
	//loading对话框;
	function window(mc:MovieClip, loadMC:MovieClip) {
		_mc = mc;
		_loadMC = loadMC;
		if (_mc == null) {
			_mc = _root;
		}
		picListenner = new Object();
		_Loader = new MovieClipLoader();
		mTextFormat = new TextFormat();
		mTextFormat.size = 10;
		mTextFormat.font = "Verdana";
		mTextFormat.color = 0x999999;
	}
	public function create(Cont:String, data:String, Tit:String, S:Number, HColor):Void {
		var _this = this;
		_this.S = S;
		if (_this.S == null) {
			_this.S = 0;
		}
		_this.HColor = HColor;
		_this.Cont = Cont;
		_this.data = data;
		_this.Tit = Tit;
		_mc.createEmptyMovieClip("win", _mc.getNextHighestDepth());
		_mc.createEmptyMovieClip("head", _mc.getNextHighestDepth());
		var Content:MovieClip = _mc.win.createEmptyMovieClip("Content", _mc.win.getNextHighestDepth());
		with (Content) {
			_x = 5;
			_y = 35;
			_this.loading(Cont, _this._mc.win.Content);
			//加载内容板块;
		}
	}
	function Shadow(mc:MovieClip) {
		var sha:DropShadowFilter = new DropShadowFilter();
		sha.color = 0x999999;
		sha.strength = .5;
		sha.blurX = 5;
		sha.blurY = 5;
		sha.angle = 45;
		sha.distance = 1;
		mc.filters = [sha];
	}
	public function loading(url:String, _mc:MovieClip) {
		//加载窗体的内容;
		var _this = this;
		_Loader.loadClip(url, _mc);
		picListenner.onLoadStart = function(evt) {
			_this._loadMC._visible = true;
			_this._loadMC.swapDepths(_root.getNextHighestDepth());
			//这边我需要的是最高级的最高层;
			//trace("loading Start");
		};
		picListenner.onLoadProgress = function(evt, LoadBytes, TotalBytes) {
			_this._loadMC.loadText.text = _this.Tit+" "+(int(LoadBytes/TotalBytes*100)+"%").toString();
			_this._loadMC.loadText.setTextFormat(_this.mTextFormat);
		};
		picListenner.onLoadComplete = function(evt) {
			//trace("加载完成");
		};
		picListenner.onLoadInit = function(evt) {
			_this._loadMC._visible = false;
			_this.H = evt._height;
			_this.W = evt._width;
			evt.loadxml(_this.data);
			evt._lockroot = true;
			_this.GDI((_this.W)+10, (_this.H)+50, _this.S, _this.HColor);
		};
		picListenner.onLoadError = function(mc, errorcode) {
			//trace(errorcode);
		};
		_Loader.addListener(picListenner);
	}
	function GDI(W:Number, H:Number, S:Number, HColor) {
		var _this = this;
		with (_this._mc.win) {
			lineStyle(1, 0x4a6d8e, 40);
			beginFill(0xffffff, 100);
			moveTo(0, 0);
			lineTo(W, 0);
			lineTo(W, H-S);
			curveTo(W, H, W-S, H);
			lineTo(S, H);
			curveTo(0, H, 0, H-S);
			//lineTo(0,H-S)
			lineTo(0, S);
			endFill();
		}
		with (_this._mc.head.createEmptyMovieClip("head", this.getNextHighestDepth())) {
			//trace(_this._mc.head.head);
			//lineStyle(1, 0xffffff, 0);
			colors = [HColor, HColor];
			alphas = [0, 100];
			ratios = [0, 0xFF];
			//matrix = {matrixType:"box", x:X, y:Y, w:(W+X)/2, h:10, r:Math.PI/2};
			matrix = {a:W, b:0, c:0, d:0, e:0, f:0, g:W*2/3, h:0, i:0};
			beginGradientFill("linear", colors, alphas, ratios, matrix);
			moveTo(5, 5);
			lineTo(W-5, 5);
			lineTo(W-5, 25);
			lineTo(5, 25);
			endFill();
		}
		Shadow(_mc.win);
		loadHeadItem(W);
	}
	function loadHeadItem(W:Number) {
		var _this = this;
		with (_this._mc.head) {
			var close:MovieClip = attachMovie("close", "close", getNextHighestDepth());
			var scale:MovieClip = attachMovie("scale", "scale", getNextHighestDepth());
			createTextField("Title", getNextHighestDepth(), 10, 5, 200, 24);
			//标题栏;
			if (_this.Tit == null) {
				Title.text = "Title";
			} else {
				Title.text = _this.Tit;
			}
			Title.selectable = false;
			Title.setTextFormat(_this.mTextFormat);
			with (close) {
				_x = W-20;
				_y = 15;
				close.onPress = function() {
					_this.Clear();
				};
			}
			with (scale) {
				//trace("crlTExt"+scale);
				_x = W-40;
				_y = 15;
				var Transparent;
				scale.onPress = function() {
					this.enabled = false;
					if (!_this.isScale) {
						_this._mc.win.Content._visible = false;
						Transparent = new Tween(_this._mc.win, "_yscale", Strong.easeOut, 100, 10, 24, false);
						new Tween(this, "_rotation", Strong.easeOut, 0, 180, 12, false);
						//_this._Loader.unloadClip(_this._mc.win.Content);
						_this._mc.win.onEnterFrame = function() {
							if (this._height<=40) {
								//trace("is <40");
								Transparent.stop();
								_this.isScale = true;
								_this._mc.head.scale.enabled = true;
								delete this.onEnterFrame;
							}
						};
						Transparent.onMotionFinished = function() {
							_this.isScale = true;
							_this._mc.head.scale.enabled = true;
							delete _this._mc.win.onEnterFrame;
						};
					} else {
						Transparent = new Tween(_this._mc.win, "_yscale", Strong.easeOut, 10, 100, 24, false);
						new Tween(this, "_rotation", Strong.easeOut, 180, 0, 12, false);
						Transparent.onMotionFinished = function() {
							_this._mc.win.Content._visible = true;
							new Tween(_this._mc.win.Content, "_alpha", Strong.easeOut, 10, 100, 24, false);
							_this.isScale = false;
							_this._mc.head.scale.enabled = true;
						};
					}
				};
			}
		}
		//end with head;
		DragHead();
	}
	function DragHead() {
		var _this = this;
		_mc.head.head.useHandCursor = false;
		//不用手形;
		_mc.head.head.onPress = function() {
			this._parent._parent.startDrag();
			this._parent._parent._alpha = 30;
			mx.behaviors.DepthControl.bringToFront(_this._mc);
		};
		_mc.head.head.onRelease = _mc.head.head.onReleaseOutside=function () {
			this._parent._parent.stopDrag();
			this._parent._parent._alpha = 100;
		};
	}
	function Clear() {
		//清理窗体;
		var _this = this;
		_Loader.unloadClip(_mc);
		_this._mc.head.removeMovieClip();
		_this._mc.head.clear();
		_this._mc.win.removeMovieClip();
		_this._mc.win.clear();
		trace(_this._mc.getDepth());
		for (var i in _root.arr) {
			if (_root.arr[i].Tit == _this.Tit) {
				_root.arr.splice(i, 1);
				//删除数组中的记录;
			}
			trace(_root.arr[i].Tit);
		}
		//trace("\n"+_this._mc.getDepth());
		_this._mc.swapDepths(1000);
		//trace(_this._mc.getDepth()+"\n");
		_this._mc.removeMovieClip();
	}
	function moveTo(X:Number, Y:Number) {
		_mc._x = X;
		_mc._y = Y;
	}
}
